<script setup>

import { ref, onMounted } from 'vue';
import {useRouter} from "vue-router";
import {getLocalStorage, getSessionStorage, removeLocalStorage} from "../main.js";
import axios from "axios";
const foodList = ref([]);
const router=useRouter();
const businessId = ref();
const user = ref({});
const deliveryAddressArr = ref([]);

onMounted(() => {
  user.value = getSessionStorage('user');
  businessId.value=router.currentRoute.value.query.businessId
  listDeliveryAddressByUserId();
});

function sexFilter(num){
  return num==1?"先生":"女士"
}

const listDeliveryAddressByUserId = () => {
  axios({
    method:"GET",
    url:'/deliveryaddress/listDeliveryAddressByUserId',
    params:{
      userId:user.value.userId
    }
  }).then( (response)=>{
    deliveryAddressArr.value = response.data;

  }).catch(function (error){
    console.log(error)
  })

};

const setDeliveryAddress = (deliveryAddress) => {
  localStorage.setItem(user.value.userId, JSON.stringify(deliveryAddress));
  router.push({ path: '/order',query:{businessId:businessId.value}},);
};

const toAddUserAddress = () => {
  router.push({ path: '/addUserAddress',query:{businessId:businessId.value} });
};

const editUserAddress = (address) => {
  router.push({ path: '/editUserAddress',query:{businessId:businessId.value,daId:address.daId} });
};

const removeUserAddress = (address) => {
  if (!window.confirm('确认要删除此送货地址吗？')) {
    return;
  }

  axios({
    method:"POST",
    url:'/deliveryaddress/removeDeliveryAddress',
    data: address
  }).then( (response)=>{
    if (response.data > 0) {
      let deliveryAddress = getLocalStorage(user.value.userId);
      if (deliveryAddress != null && deliveryAddress.daId == address.daId) {
        removeLocalStorage(user.value.userId);

      }
      listDeliveryAddressByUserId();
    } else {
      alert('删除地址失败！');
    }

  }).catch(function (error){
    console.log(error)
  })

  // $axios.post('DeliveryAddressController/removeDeliveryAddress', $qs.stringify({
  //   daId: daId
  // })).then(response => {
  //   if (response.data > 0) {
  //     let deliveryAddress = getLocalStorage(user.value.userId);
  //     if (deliveryAddress != null && deliveryAddress.daId == daId) {
  //       removeLocalStorage(user.value.userId);
  //     }
  //     listDeliveryAddressByUserId();
  //   } else {
  //     alert('删除地址失败！');
  //   }
  // }).catch(error => {
  //   console.error(error);
  // });
};
</script>

<template>
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <p>地址管理</p>
    </header>
    <!-- 地址列表部分 -->
    <ul class="addresslist">
      <li v-for="item in deliveryAddressArr">
        <div class="addresslist-left" @click="setDeliveryAddress(item)">
          <h3>{{item.contactName}}{{sexFilter(item.contactSex)}} {{item.contactTel}}
          </h3>
          <p>{{item.address}}</p>
        </div>
        <div class="addresslist-right">
          <img src="../assets/icons/userAddress/edit.png" @click="editUserAddress(item)">
          <img src="../assets/icons/userAddress/delete.png" @click="removeUserAddress(item)">
        </div>
      </li>
    </ul>
    <!-- 新增地址部分 -->
    <div class="addbtn" @click="toAddUserAddress">
      <i class="fa fa-plus-circle"></i>
      <p>新增收货地址</p>
    </div>
    <!-- 底部菜单部分 -->

  </div>
</template>





<style scoped>
/*************** 总容器 ***************/
.wrapper {
  width: 100%;
  height: 100%;
  background-color: #F5F5F5;
}
/*************** header ***************/
.wrapper header {
  width: 100%;
  height: 12vw;
  background-color: #0097FF;
  display: flex;
  justify-content: space-around;
  align-items: center;
  color: #fff;
  font-size: 4.8vw;
  position: fixed;
  left: 0;
  top: 0;
  /*保证在最上层*/
  z-index: 1000;
}
/*************** addresslist ***************/
.wrapper .addresslist {
  width: 100%;
  margin-top: 12vw;
  background-color: #fff;
}
.wrapper .addresslist li {
  width: 100%;
  box-sizing: border-box;
  border-bottom: solid 1px #DDD;
  padding: 3vw;
  display: flex;
}
.wrapper .addresslist li .addresslist-left {
  flex: 5;
  /*左边这块区域是可以点击的*/
  user-select: none;
  cursor: pointer;

}
.wrapper .addresslist li .addresslist-left h3 {
  font-size: 4.6vw;
  font-weight: 300;
  color: #666;
}
.wrapper .addresslist li .addresslist-left p {
  font-size: 4vw;
  color: #666;
}
.wrapper .addresslist li .addresslist-right {
  flex: 1;
  font-size: 5.6vw;
  color: #999;
  cursor: pointer;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.wrapper .addresslist li .addresslist-right img{
  margin-right: 3vw;
  width: 8vw;
  height: 8vw;
}
/*************** 新增地址部分 ***************/
.wrapper .addbtn {
  width: 100%;
  height: 14vw;
  border-top: solid 1px #DDD;
  border-bottom: solid 1px #DDD;
  background-color: #fff;
  margin-top: 4vw;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 4.5vw;
  color: #0097FF;
  user-select: none;
  cursor: pointer;
}
.wrapper .addbtn p {
  margin-left: 2vw;
}
</style>
